package org.unidbfw.db.core;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class DbTable
{

	protected String[] titles = null;
	protected Set<DbRow> rows = null;
	
	public DbTable()
	{
		rows = new HashSet<DbRow>();
	}
	
	public DbTable(String[] titles)
	{
		this.titles = titles;
	}
	
	public DbTable(Collection<DbRow> rows)
	{
		this.rows = new HashSet<DbRow>(rows);
		for (DbRow row: rows)
		{
			titles = new String[rows.size()];
			titles = row.getTitles().toArray(titles);
			break;
		}
	}
	
	public DbTable(Collection<DbRow> rows, String[] titles)
	{
		this.rows = new HashSet<DbRow>(rows);
		this.titles = titles;
	}
	
	public void updateRows(DbRow row)
	{
		updateRows(Collections.singleton(row));
	}
	
	public void updateRows(Collection<DbRow> rows){
		for (DbRow row: rows)
		{
			if (titles == null)
			{
				titles = new String[rows.size()];
				titles = row.getTitles().toArray(titles);
			}
			this.rows.add(row);
		}
	}
	
	public void deleteRows(DbRow row)
	{
		deleteRows(Collections.singleton(row));
	}
	
	public void deleteRows(Collection<DbRow> rows)
	{
		for (DbRow row: rows)
		{
			this.rows.remove(row);
		}
	}
	
	public Set<DbRow> getRows()
	{
		return rows;
	}
	
	public String[] getTitles()
	{
		return titles;
	}
	
}
